<?php


namespace App\Models\Admin;


use App\Log\DocumentLog;

class Document extends Base
{
    const OPERATE_PID = 4;
    const MODEL = 'document';

    protected $table = 'document';
    protected $guarded = [];
    protected $dateFormat = 'U';

    public function companyType()
    {
        return $this->belongsTo(CompanyType::class,'type','id');
    }

    public function menuFirst()
    {
        return $this->belongsTo(Menu::class,'first_menu_id','id')->select('id','name');
    }

    public function menuSec()
    {
        return $this->belongsTo(Menu::class,'sec_menu_id','id')->select('id','name');
    }

    public function product()
    {
        return $this->belongsTo(AdminProduct::class,'product_type','id')->select('id','name');
    }

    public static function addLog($pre,$next,$type,$method='')
    {
        $info = AdminOperateModule::where('pid',self::OPERATE_PID)->where('type',$type)->where('model',self::MODEL)->when($method,function ($query)use ($method){
            $query->where('method',$method);
        })->first();
        if (!empty($info)){
            $adminLog = new DocumentLog($pre,$next,$info);
            $adminLog->addLog();
        }
    }
}
